var rentcity="";
var startDate="";
var endDate="";
var starthour="";
var endhour="";
var starttime="";
var endtime="";
var carid="";
var serviceid="";
var contactsid="";
var score=0;
var basePath="";
var rentorderid="";
var allscore="";
var totalprice="";
var name = "";
var identtype = "";
var identnum = "";
var email = "";
var phone = "";
var cusmsg = "";
var token = "";
$(document).ready(function(){
	
	loadP();
	wantN();
	wantM();
	bindP();
	warningD();
	HandwithScoer();
	GoToRule();
});

/**
 * 兼容IE
 */
function NewDate(str) { 
      //首先将日期分隔 ，获取到日期部分 和 时间部分
      var day = str.split(' ');
      //获取日期部分的年月日
      var days = day[0].split('-');
      //获取时间部分的 时分秒
      var mi = day[day.length - 1].split(':');
      //获取当前date类型日期
      var date = new Date();
      //给date赋值  年月日
      date.setUTCFullYear(days[0], days[1] - 1, days[2]);
      //给date赋值 时分秒  首先转换utc时区 ：+8
      date.setUTCHours(mi[0] - 8, mi[1], mi[2]);
      return date;
};

//初始化页面携带过来的参数
function loadP(){
	rentcity=$("div.rentorder_parameter>div:eq(0)").text();
	startDate=$("div.rentorder_parameter>div:eq(1)").text();
	endDate=$("div.rentorder_parameter>div:eq(2)").text();
	carid=$("div.rentorder_parameter>div:eq(3)").text();
	serviceid=$("div.rentorder_parameter>div:eq(4)").text();
	serviceid = serviceid.replace("-","|");
	starthour=$("div.rentorder_parameter>div:eq(5)").text();
	endhour=$("div.rentorder_parameter>div:eq(6)").text();
	token = $("div.rentorder_parameter>div:eq(7)").text();
	
	var st = startDate + " " + starthour + ":00";
	var et = endDate + " " + endhour + ":00";

	var stt = NewDate(st);
	var ent = NewDate(et);
	console.log(stt,111)
	console.log(ent,222)
	var sth = stt.getHours();
	var stm = stt.getMinutes();
	var enh = ent.getHours();
	var enm = ent.getMinutes();
	if(stm == 30){
		starttime = sth*2 + 1;
	}else{
		starttime = sth*2;
	}
	if(enm == 30){
		endtime = enh*2 + 1;
	}else{
		endtime = enh*2;
	}
	console.log("startDate="+startDate);
	console.log("endDate="+endDate);
	console.log("starttime="+starttime);
	console.log("endtime="+endtime);
	console.log("carid="+carid);
	console.log("serviceid="+serviceid);
	wantS(carid,startDate,endDate,starttime,endtime,serviceid);
	CountRentTime();
}
//计算出发返程时间差
function CountRentTime(){
	var startdate = $("div.rentorder_parameter>div:eq(1)").text();
	var enddate = $("div.rentorder_parameter>div:eq(2)").text();
	var starthour = $("div.rentorder_parameter>div:eq(5)").text();
	var endhour = $("div.rentorder_parameter>div:eq(6)").text();
	var sd = startdate + " " + starthour + ":00";
	var ed = enddate + " " + endhour + ":00";
	//处理时间
	var stime = NewDate(sd);
	var etime = NewDate(ed);
	var usedTime = etime - stime;
	var days=Math.floor(usedTime/(24*3600*1000));  
    //计算出小时数  
    var leave1=usedTime%(24*3600*1000);    //计算天数后剩余的毫秒数  
    var hours=Math.floor(leave1/(3600*1000));  
    //计算相差分钟数  
    var leave2=leave1%(3600*1000);        //计算小时数后剩余的毫秒数  
    var minutes=Math.floor(leave2/(60*1000));  
	
	var up = days + "天";
	var down = hours + "小时" + minutes + "分钟";
	var counttime = "共" + up + down;
	$("div.rentorder_top_time_center_count").text(counttime);
	
}
//请求常用联系人信息
function wantN(){
	
	$.ajax({	
		dataType : "json",
		type : "post",
		url : basePath+"/meb/getcontact",
		data : {
			
		},
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		success : function(data) {
			var p="";
			for(var i=0;i<data.data.length;i++){
				if(data.data[i].name!=""&&data.data[i].name!=null){
					p=p+"<div class=\"rentorder_center_common_two_item\">"+data.data[i].name+"</div>\n"+
					"<font class=\"hidden\">"+data.data[i].id+"</font>"
				}
			}
			$("div.rentorder_center_common_two").html(p);
			if(p == ""){
				$("div.rentorder_center_common").addClass("hidden");
			};
			chooseP();
			
		},
		error : function() {
			console.log("访问失败");
		},
		complete : function() {
			
		},
	});
}
//绑定常用联系人点击效果
function chooseP(){
	var l=$("div.rentorder_center_common_two_item").length;
	for(var i=0;i<l;i++){
		$("div.rentorder_center_common_two_item:eq("+i+")").click(function(i){
			return function(){
				var id=$("div.rentorder_center_common_two>font:eq("+i+")").text();
				contactsid=id;
				wantPI(id);
				console.log("id="+id);
			}			
		}(i))
	};
}
//请求常用联系人详细信息
function wantPI(id){
	LoadingIcon = layer.load();
	$.ajax({	
		dataType : "json",
		type : "post",
		url : basePath+"/meb/getcontactinfo",
		data : {
			id:id
		},
		
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		success : function(data) {
			var name=data.data[0].name;
			var identtype=data.data[0].identtype;
			var identnum=data.data[0].identnum;
			var email=data.data[0].email;
			var phone=data.data[0].phone;
			$("input.rentorder_center_detail_item_two:eq(0)").val(name);
			$("input.rentorder_center_detail_item_two:eq(2)").val(identnum);
			$("input.rentorder_center_detail_item_two:eq(3)").val(phone);
			$("input.rentorder_center_detail_item_two:eq(4)").val(email);
			console.log("name="+name);
			console.log("identnum="+identnum);
			console.log("phone="+phone);
			console.log("email="+email);
			
			
		},
		error : function() {
			console.log("访问失败");
		},
		complete : function() {
			layer.close(LoadingIcon);
		},
	});
}
//请求车辆详情信息
function wantM(){
	LoadingIcon = layer.load();
	$.ajax({	
		dataType : "json",
		type : "post",
		url : basePath+"/car/orderinfo",
		data : {
			carid:carid
		},
		
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		success : function(data) {
			var title=data.data.title;
			var url=data.data.showurl;
			var passengers=data.data.passengers;
			var oilwear=data.data.oilwear;
			var transmission=data.data.transmission;
			var oiltype=data.data.oiltype;
			var license=data.data.license;
			var drive=data.data.drive;
			var bed=data.data.bed;
			var bedinfo=data.data.bedinfo;
			console.log("url="+url);
			$("[data-toggle='tooltip']").tooltip();//启动tooltip提示工具
			$("img.rentorder_top_info_pic_posi").attr("src",url);
			$("div.rentorder_top_info_detail_title").text(title);
			$("div.rentorder_top_info_detail_info_item_info:eq(0)").text(passengers);
			$("div.rentorder_top_info_detail_info_item_info_oil").text(oilwear);
			$("div.rentorder_top_info_detail_info_item_info_oil").attr('title',oilwear).tooltip('fixTitle');
			$("div.rentorder_top_info_detail_info_item_info:eq(1)").text(transmission);
			$("div.rentorder_top_info_detail_info_item_info:eq(2)").text(oiltype);
			$("div.rentorder_top_info_detail_info_item_info:eq(3)").text(license);
			$("div.rentorder_top_info_detail_info_item_info:eq(4)").text(drive);
			$("div.rentorder_top_info_detail_info_item_info:eq(5)").text(bed);
			$("div.rentorder_top_info_detail_info_bedinfo").text(bedinfo);
			//设置title
			$(document).attr("title","订单填写_"+title);//修改title值  
		},
		error : function() {
			console.log("访问失败");
		},
		complete : function() {
			layer.close(LoadingIcon);
		},
	});
}
//请求服务价格
function wantS(carid,startDate,endDate,starttime,endtime,serviceid){
	
	$.ajax({	
		dataType : "json",
		type : "post",
		url : basePath+"/car/priceinfo",
		data : {
			id:carid,
			startDate:startDate,
			endDate:endDate,
			starttime:starttime,
			endtime:endtime,
			serveid:serviceid,
		},
		
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		success : function(data) {
			var x="";
			var t=0;
			for(var i=0;i<data.data.length;i++){
				x=x+
				"<div class=\"col-lg-3 col-md-3 rentorder_down_two_item_one\">"+data.data[i].serve+"</div>\n"+
				"<div class=\"col-lg-7 col-md-7 rentorder_down_two_item_two\"></div>\n"+
				"<div class=\"col-lg-2 col-md-2 rentorder_down_two_item_three clearfix\">\n"+
					"<div class=\"rentorder_down_two_item_three_symbol\">￥</div>\n"+
					"<div class=\"rentorder_down_two_item_three_money\">"+data.data[i].price+"</div>\n"+
				"</div>";
				t=t+data.data[i].price;
			};
			totalprice = t;
			$("div.rentorder_down_two_item:eq(0)").html(x);
			$("div.rentorder_down_four_price_money").html(t);
			WantScore();//只有请求完知道总价格之后才可以比对积分
			
		},
		error : function() {
			console.log("访问失败");
		},
		complete : function() {
			
		},
	});
}
//请求跳转租车支付页
function gotoP(startDate,endDate,starttime,endtime,carid,serviceid,contactsid,name,identtype,identnum,email,phone,cusmsg,score){
	console.log("startDate="+startDate);
	console.log("endDate="+endDate);
	console.log("starttime="+starttime);
	console.log("endtime="+endtime);
	console.log("carid="+carid);
	console.log("serviceid="+serviceid);
	console.log("contactsid="+contactsid);
	console.log("name="+name);
	console.log("identtype="+identtype);
	console.log("identnum="+identnum);
	console.log("email="+email);
	console.log("phone="+phone);
	console.log("cusmsg="+cusmsg);
	console.log("score="+score);
	$.ajax({	
		dataType : "json",
		type : "post",
		url : basePath+"/car/order",
		data : {
			startDate:startDate,
			endDate:endDate,
			starttime:starttime,
			endtime:endtime,
			id:carid,
			serveid:serviceid,
			contactsid:contactsid,
			name:name,
			identtype:identtype,
			identnum:identnum,
			email:email,
			phone:phone,
			cusmsg:cusmsg,
			score:score,
		
		},
		
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		success : function(data) {
			rentorderid = data.data.orderid;
//			alert("rentorderid="+data.data.orderid);
			window.location.href = (basePath+"/rentpay?rentorderid="+rentorderid);

			
		},
		error : function() {
			console.log("访问失败");
		},
		complete : function() {
			
		},
	});
}
//判定是否能够跳转支付页面
function canNotG(){
	var name=$("input.rentorder_center_detail_item_two:eq(0)").val();
	var identtype=$("input.rentorder_center_detail_item_two:eq(2)").val();
	var identnum=$("input.rentorder_center_detail_item_two:eq(3)").val();
	if(identnum==""||identnum==null){
		console.log("identnum="+identnum);
		$("div.rentorder_center_detail_item_three:eq(3)").toggle(function(){
			$(this).attr("style","display:block;");//动态显示div
			alert("请输入正确的信息");
		});
		return false;
	}
	if(identtype==""||identtype==null){
		console.log("identtype="+identtype);
		$("div.rentorder_center_detail_item_three:eq(2)").toggle(function(){
			$(this).attr("style","display:block;");//动态显示div
			alert("请输入正确的信息");
		});
		return false;
	}
	if(name==""||name==null){
		console.log("name="+name);
		$("div.rentorder_center_detail_item_three:eq(0)").toggle(function(){
			$(this).attr("style","display:block;");//动态显示div
			alert("请输入正确的信息");
		});
		return false;
	}
	return true;
	
}
//绑定跳转支付页面的动作
function bindP(){
	
	$("input.rentorder_down_four_order_b").click(function(){
		name=$("input.rentorder_center_detail_item_two:eq(0)").val();
		identtype=$("input.rentorder_center_detail_item_two:eq(1)").val();
		identnum=$("input.rentorder_center_detail_item_two:eq(2)").val();
		phone=$("input.rentorder_center_detail_item_two:eq(3)").val();
		email=$("input.rentorder_center_detail_item_two:eq(4)").val();
		cusmsg=$("input.rentorder_center_detail_item_two:eq(5)").val();
		if(canNotG()){
			var loading = layer.load(1,{time:10*1000});//10秒loading，防止用户快速点击
			gotoP(startDate,endDate,starttime,endtime,carid,serviceid,contactsid,name,identtype,identnum,email,phone,cusmsg,score);
//			var rentorderid = "03261d4b878642c1b20603467acbe7d8";//模拟数据
//			window.location.href = (basePath+"/rentpay?rentorderid="+rentorderid);
		}
	});	
}
//点击input提醒信息会消失
function warningD(){
	for(var i=0;i<4;i++){
		if(i==1){
			continue;
		}
		$("input.rentorder_center_detail_item_two:eq("+i+")").focus(function(i){
			return function(){
				$("div.rentorder_center_detail_item_three:eq("+i+")").css("display","none");
			}		
		}(i))
	}	
}
//请求积分
function WantScore(){
	var token = $("div.allhead_userinfo_token").text();
	console.log("token=" + token);
	$.ajax({	
		dataType : "json",
		type : "post",
		url : basePath+"/car/rentorderscore",
		data : {
			token:token	
		},
		
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		success : function(data) {
			var s= data.data.score;
			allscore = s;
			console.log("积分为--------------");
			console.log("allscore="+allscore);
			FristHandwithScoer();
		},
		error : function() {
			console.log("访问失败");
		},
		complete : function() {
			
		},
	});
}
//首次对积分进行操作
function FristHandwithScoer(){
	var scoreCount = allscore * 0.01;
	var cscore = 0;
	if(scoreCount >= totalprice){
		if(totalprice >= 1000){
				
				if($("input#useScore").is(":checked")){//检查是否选中使用积分
					cscore = 100000;
					score = allscore;
				}else{
					cscore = 0;
					score = 0;
				};			
				var wordOne = "使用积分抵用"+ 1000 +"元";
				var wordTwo = "当前积分" + allscore + "可用积分" + 100000;
				$("div.rentorder_down_two_item_two_score_up").text(wordOne);
				$("div.rentorder_down_two_item_two_score_down").text(wordTwo);
				var showprice = totalprice - cscore*0.01;
				$("div.rentorder_down_four_price_money").text(showprice);
				var showscorecount = cscore * 0.01;
				$("div#showscorecount").text(showscorecount);
				console.log("fone");
		}else if(totalprice < 1000){
			
				if($("input#useScore").is(":checked")){
					cscore = totalprice * 100;
					score = allscore;
				}else{
					cscore = 0;
					score = 0;
				};
				var wordOne = "使用积分抵用" + totalprice + "元";
				var wordTwo = "当前积分" + allscore + "可用积分" + totalprice * 100;
				$("div.rentorder_down_two_item_two_score_up").text(wordOne);
				$("div.rentorder_down_two_item_two_score_down").text(wordTwo);
				var showprice = totalprice - cscore*0.01;
				$("div.rentorder_down_four_price_money").text(showprice);
				var showscorecount = cscore * 0.01;
				$("div#showscorecount").text(showscorecount);
				console.log("ftwo");
		};
		
	}else if(scoreCount < totalprice){
		if(scoreCount >= 1000){
			
				if($("input#useScore").is(":checked")){
					cscore = 100000;
					score = allscore;
				}else{
					cscore = 0;
					score = 0;
				};
				var wordOne = "使用积分抵用" + 1000 + "元";
				var wordTwo = "当前积分" + allscore + "可用积分" + 100000;
				$("div.rentorder_down_two_item_two_score_up").text(wordOne);
				$("div.rentorder_down_two_item_two_score_down").text(wordTwo);
				var showprice = totalprice - cscore*0.01;
				$("div.rentorder_down_four_price_money").text(showprice);
				var showscorecount = cscore * 0.01;
				$("div#showscorecount").text(showscorecount);
				console.log("fthree");
		}else if(scoreCount < 1000){
			
				if($("input#useScore").is(":checked")){
					cscore = allscore;
					score = allscore;
				}else{
					cscore = 0;
					score = 0;
				};
				var wordOne = "使用积分抵用" + allscore*0.01 + "元";
				var wordTwo = "当前积分" + allscore + "可用积分" + allscore;
				$("div.rentorder_down_two_item_two_score_up").text(wordOne);
				$("div.rentorder_down_two_item_two_score_down").text(wordTwo);
				var showprice = totalprice - cscore*0.01;
				$("div.rentorder_down_four_price_money").text(showprice);
				var showscorecount = cscore * 0.01;
				$("div#showscorecount").text(showscorecount);
				console.log("ffour");
		};
	};
	console.log("score="+score);
	console.log("allscore="+allscore);
	console.log("totalprice="+totalprice);
}
//对积分进行操作
function HandwithScoer(){
	$("input#useScore").click(function(){
		FristHandwithScoer();
	});
}
//点击规则跳转规则页
var GoToRule = function(){
	$("div.rentorder_down_five_text").click(function(){
		window.open(basePath+"/rentandservicerule"); 
	});
}